<div class="tabs_sort_by_title">Sort by</div>
<div class="tabs_sort_by">
    <ul>
        <?php
        foreach ($sorts as $s) {
            if ($s['value'] == $sort) {
                echo '<li value="' . $s['value'] . '" class="active">' . $s['name'] . '</li>';
            } else {
                echo '<li value="' . $s['value'] . '"><a href="' . url_for('hotels', array('page' => $page, 'sort' => $s['value'])) . '">' . $s['name'] . '</a></li>';
            }
        }
        ?>
    </ul>
    <hr>
</div>

<?php
$next_page;
$back_page;

if ($page == 1) {
    $back_page = $page;
} else {
    $back_page = $page - 1;
}

if ($pages == $page) {
    $next_page = $page;
} else {
    $next_page = $page + 1;
}
?>

<div class="pages_list">
    <ul>
        <li><a href="<?php echo url_for('hotels', array('page' => $back_page, 'sort' => $sort)) ?>"><<</a></li>
        <?php
        if ($pages > 10) {
            if ($page < 6) {
                for ($i = 1; $i < 9; $i++) {
                    if ($i == $page) {
                        echo '<li class="active"><a href="' . url_for('hotels', array('page' => $i, 'sort' => $sort)) . '">' . $i . '</a></li>';
                    } else {
                        echo '<li><a href="' . url_for('hotels', array('page' => $i, 'sort' => $sort)) . '">' . $i . '</a></li>';
                    }
                }

                echo '<li>...</li>';
                echo '<li><a href="' . url_for('hotels', array('page' => $pages, 'sort' => $sort)) . '">' . $pages . '</a></li>';
            } else if ($pages - $page < 5) {
                echo '<li><a href="' . url_for('hotels', array('page' => 1, 'sort' => $sort)) . '">1</a></li>';
                echo '<li>...</li>';

                for ($i = ($pages - 7); $i <= $pages; $i++) {
                    if ($i == $page) {
                        echo '<li class="active"><a href="' . url_for('hotels', array('page' => $i, 'sort' => $sort)) . '">' . $i . '</a></li>';
                    } else {
                        echo '<li><a href="' . url_for('hotels', array('page' => $i, 'sort' => $sort)) . '">' . $i . '</a></li>';
                    }
                }
            } else {
                echo '<li><a href="' . url_for('hotels', array('page' => 1, 'sort' => $sort)) . '">1</a></li>';
                echo '<li>...</li>';

                for ($i = ($page - 2); $i < $page; $i++) {
                    if ($i == $page) {
                        echo '<li class="active"><a href="' . url_for('hotels', array('page' => $i, 'sort' => $sort)) . '">' . $i . '</a></li>';
                    } else {
                        echo '<li><a href="' . url_for('hotels', array('page' => $i, 'sort' => $sort)) . '">' . $i . '</a></li>';
                    }
                }

                for ($i = $page; $i < $page + 4; $i++) {
                    if ($i == $page) {
                        echo '<li class="active"><a href="' . url_for('hotels', array('page' => $i, 'sort' => $sort)) . '">' . $i . '</a></li>';
                    } else {
                        echo '<li><a href="' . url_for('hotels', array('page' => $i, 'sort' => $sort)) . '">' . $i . '</a></li>';
                    }
                }

                echo '<li>...</li>';
                echo '<li><a href="' . url_for('hotels', array('page' => $pages, 'sort' => $sort)) . '">' . $pages . '</a></li>';
            }
        } else {
            for ($i = 1; $i <= $pages; $i++) {
                if ($i == $page) {
                    echo '<li class="active"><a href="' . url_for('hotels', array('page' => $i, 'sort' => $sort)) . '">' . $i . '</a></li>';
                } else {
                    echo '<li><a href="' . url_for('hotels', array('page' => $i, 'sort' => $sort)) . '">' . $i . '</a></li>';
                }
            }
        }
        ?>
        <li><a href="<?php echo url_for('hotels', array('page' => $next_page, 'sort' => $sort)) ?>">>></a></li>
    </ul>
</div>

<div class="hotel_container">
    <div class="hotel_blocks">
        <?php
        foreach ($hotels as $h) {
            $image = $h['photo'];
            if (is_null($image)) {
                $image = '../uploads/no_image.png';
            } else {
                $image = explode(';', $image);
                $image = $image[0];
            }

            $stars = array(
                '/web/images/icoStar.png',
                '/web/images/icoStars2.png',
                '/web/images/icoStars3.png',
                '/web/images/icoStars4.png',
                '/web/images/icoStars5.png'
            );
            echo '<div class="row_one_container"><div class="row_two_container"><div class="row_three_container">'
            . '<div class="image_container_hotel"><a href="' . url_for('hotel', array('id' => $h['id'])) . '"><img src="' . $image . '"/></a><div>' . $h['description_en'] . '</div></div>'
            . '<div class="rooms_container_hotel"><div class="rooms_title"><div class="hotel_description">';
            if ($h['stars'] > 0) {
                echo '<div class="hotel_name"><a href="' . url_for('hotel', array('id' => $h['id'])) . '">' . $h['name_en'] . '</a><img src="' . $stars[$h['stars'] - 1] . '"/></div>';
            } else {
                echo '<div class="hotel_name"><a href="' . url_for('hotel', array('id' => $h['id'])) . '">' . $h['name_en'] . '</a></div>';
            }
            echo '<div class="hotel_location_one">' . $h['CountryHotel']['name_en'] . ' ' . $h['RegionHotel']['name_en'] . '</div>'
            . '<div class="hotel_location_two">' . $h['CityHotel']['name_en'] . '</div></div></div>'
            . '<div class="rooms_title_description">'
            . '<div class="rtd_room">Room</div>'
            . '<div class="rtd_people">Adult/Children/Babies</div>'
            . '<div class="rtd_boarding">Usual Boarding</div>'
            . '<div class="rtd_price">Price</div></div>';

            if (count($h['Rooms']) > 0) {
                $prices = array();
                foreach ($h['Rooms'] as $r) {
                    echo '<div class="rooms_full_description">'
                    . '<div class="rfd_room" title="' . $r['description_en'] . '">' . $r['name'] . '</div>'
                    . '<div class="rfd_people">' . $r['adult'] . ' | ' . $r['children'] . ' | ' . $r['baby'] . '</div>'
                    . '<div class="rfd_boarding"></div>'
                    . '<div class="rfd_price">' . $r['RoomPrice']['bar_price_usd'] . '</div></div>';
                    array_push($prices, $r['RoomPrice']['bar_price_usd']);
                }
            } else {
                $prices = array(0);
                echo '<div class="rooms_full_description"><div class="rfd_not_available">'
                . 'Unfortunately, there are no empty rooms for this date. You can ask the admin and we will try to help you.'
                . '</div></div>';
            }

            echo '</div><div class="price_container_hotel"><div class="price_title">'
            . '<div class="price_more_info"><a href="' . url_for('hotel', array('id' => $h['id'])) . '"><ul><li>Map</li><li> | </li><li>Hotel info</li></ul></a></div>';

            if (count($prices) > 1) {
                echo '<div class="price_avg">' . min($prices) . '$ - ' . max($prices) . '$</div></div>';
            } else if (count($prices) == 1) {
                if ($prices[0] > 0) {
                    echo '<div class="price_avg">' . $prices[0] . '$</div></div>';
                } else {
                    echo '<div class="price_avg"> --- </div></div>';
                }
            }
            
            if (count($h['Rooms']) > 0) {
                $button = array('value' => 'Reserve It', 'url' => url_for('hotel', array('id' => $h['id'], 'date' => $search['date'], 'night' => $search['night'], 'stars' => $search['stars'], 'rooms' => $search['rooms'])));
            } else {
                $button = array('value' => 'Ask the Admin', 'url' => url_for(''));
            }

            echo
            '<div class="price_total_container"><div class="vertical_seporator">'
            . '<div class="price_booking_btn"><a href="' . $button['url'] . '">' . $button['value'] . '</a></div></div></div></div></div></div></div>';
        }
        ?>
    </div>
    <div style="clear: both"></div>
</div>

<div class="pages_list">
    <ul>
        <li><a href="<?php echo url_for('hotels', array('page' => $back_page, 'sort' => $sort)) ?>"><<</a></li>
        <?php
        if ($pages > 10) {
            if ($page < 6) {
                for ($i = 1; $i < 9; $i++) {
                    if ($i == $page) {
                        echo '<li class="active"><a href="' . url_for('hotels', array('page' => $i, 'sort' => $sort)) . '">' . $i . '</a></li>';
                    } else {
                        echo '<li><a href="' . url_for('hotels', array('page' => $i, 'sort' => $sort)) . '">' . $i . '</a></li>';
                    }
                }

                echo '<li>...</li>';
                echo '<li><a href="' . url_for('hotels', array('page' => $pages, 'sort' => $sort)) . '">' . $pages . '</a></li>';
            } else if ($pages - $page < 5) {
                echo '<li><a href="' . url_for('hotels', array('page' => 1, 'sort' => $sort)) . '">1</a></li>';
                echo '<li>...</li>';

                for ($i = ($pages - 7); $i <= $pages; $i++) {
                    if ($i == $page) {
                        echo '<li class="active"><a href="' . url_for('hotels', array('page' => $i, 'sort' => $sort)) . '">' . $i . '</a></li>';
                    } else {
                        echo '<li><a href="' . url_for('hotels', array('page' => $i, 'sort' => $sort)) . '">' . $i . '</a></li>';
                    }
                }
            } else {
                echo '<li><a href="' . url_for('hotels', array('page' => 1, 'sort' => $sort)) . '">1</a></li>';
                echo '<li>...</li>';

                for ($i = ($page - 2); $i < $page; $i++) {
                    if ($i == $page) {
                        echo '<li class="active"><a href="' . url_for('hotels', array('page' => $i, 'sort' => $sort)) . '">' . $i . '</a></li>';
                    } else {
                        echo '<li><a href="' . url_for('hotels', array('page' => $i, 'sort' => $sort)) . '">' . $i . '</a></li>';
                    }
                }

                for ($i = $page; $i < $page + 4; $i++) {
                    if ($i == $page) {
                        echo '<li class="active"><a href="' . url_for('hotels', array('page' => $i, 'sort' => $sort)) . '">' . $i . '</a></li>';
                    } else {
                        echo '<li><a href="' . url_for('hotels', array('page' => $i, 'sort' => $sort)) . '">' . $i . '</a></li>';
                    }
                }

                echo '<li>...</li>';
                echo '<li><a href="' . url_for('hotels', array('page' => $pages, 'sort' => $sort)) . '">' . $pages . '</a></li>';
            }
        } else {
            for ($i = 1; $i <= $pages; $i++) {
                if ($i == $page) {
                    echo '<li class="active"><a href="' . url_for('hotels', array('page' => $i, 'sort' => $sort)) . '">' . $i . '</a></li>';
                } else {
                    echo '<li><a href="' . url_for('hotels', array('page' => $i, 'sort' => $sort)) . '">' . $i . '</a></li>';
                }
            }
        }
        ?>
        <li><a href="<?php echo url_for('hotels', array('page' => $next_page, 'sort' => $sort)) ?>">>></a></li>
    </ul>
</div>
